package com.papercutting.platform.controller.admin;

import com.papercutting.platform.common.Result;
import com.papercutting.platform.dto.query.TutorialQuery;
import com.papercutting.platform.dto.request.TutorialRequest;
import com.papercutting.platform.dto.response.PageResult;
import com.papercutting.platform.dto.response.TutorialResponse;
import com.papercutting.platform.service.TutorialService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * 管理员-教程控制器
 */
@Slf4j
@RestController("adminTutorialController")
@RequestMapping("/api/admin/tutorial")
@RequiredArgsConstructor
@Validated
public class TutorialController {
    
    private final TutorialService tutorialService;
    
    /**
     * 创建教程
     */
    @PostMapping
    public Result<Long> createTutorial(@Valid @RequestBody TutorialRequest request) {
        log.info("创建教程请求: {}", request.getTitle());
        
        Long tutorialId = tutorialService.createTutorial(request);
        
        return Result.success("教程创建成功", tutorialId);
    }
    
    /**
     * 更新教程
     */
    @PutMapping("/{id}")
    public Result<Void> updateTutorial(
            @PathVariable @NotNull Long id,
            @Valid @RequestBody TutorialRequest request) {
        log.info("更新教程请求: {}", id);
        
        request.setId(id);
        tutorialService.updateTutorial(request);
        
        return Result.success("教程更新成功");
    }
    
    /**
     * 删除教程
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteTutorial(@PathVariable @NotNull Long id) {
        log.info("删除教程请求: {}", id);
        
        tutorialService.deleteTutorial(id);
        
        return Result.success("教程删除成功");
    }
    
    /**
     * 获取教程详情
     */
    @GetMapping("/{id}")
    public Result<Map<String, Object>> getTutorialDetail(@PathVariable @NotNull Long id) {
        log.info("获取教程详情: {}", id);
        
        Map<String, Object> tutorial = tutorialService.getTutorialById(id);
        
        return Result.success(tutorial);
    }
    
    /**
     * 分页查询教程列表
     */
    @GetMapping("/page")
    public Result<PageResult<TutorialResponse>> getTutorialPage(TutorialQuery query) {
        log.info("分页查询教程列表: {}", query);
        
        PageResult<TutorialResponse> result = tutorialService.getTutorialPage(query);
        
        return Result.success(result);
    }
    
    /**
     * 获取教程列表（不分页）
     */
    @GetMapping("/list")
    public Result<List<TutorialResponse>> getTutorialList(TutorialQuery query) {
        log.info("查询教程列表: {}", query);
        
        List<TutorialResponse> result = tutorialService.getTutorialList(query);
        
        return Result.success(result);
    }
    
    /**
     * 批量删除教程
     */
    @DeleteMapping("/batch")
    public Result<Void> batchDeleteTutorials(@RequestBody List<Long> ids) {
        log.info("批量删除教程: {}", ids);
        
        for (Long id : ids) {
            tutorialService.deleteTutorial(id);
        }
        
        return Result.success("批量删除成功");
    }
    
    /**
     * 批量更新状态
     */
    @PutMapping("/batch/status")
    public Result<Void> batchUpdateStatus(@RequestBody Map<String, Object> params) {
        log.info("批量更新状态: {}", params);
        
        @SuppressWarnings("unchecked")
        List<Long> ids = (List<Long>) params.get("ids");
        Integer status = (Integer) params.get("status");
        
        // TODO: 实现批量更新状态逻辑
        
        return Result.success("批量更新状态成功");
    }
    
    /**
     * 获取统计信息
     */
    @GetMapping("/stats")
    public Result<Map<String, Object>> getTutorialStats() {
        log.info("获取教程统计信息");
        
        // TODO: 实现统计信息获取逻辑
        Map<String, Object> stats = Map.of(
            "totalCount", 0,
            "publishedCount", 0,
            "draftCount", 0,
            "totalStudyCount", 0
        );
        
        return Result.success(stats);
    }
}
